package grape.attend.service.user.api;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import grape.attend.service.user.po.UserClockDate;
import grape.attend.service.user.query.UserClockDateListQuery;
import grape.common.service.common.IBaseService;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户打卡日期表 服务类
 * </p>
 *
 * @author yangwei
 * @since 2020-05-25
 */
public interface IUserClockDateService extends IBaseService<UserClockDate> {

    /**
     *
     * @param classeDate yyyy-MM-dd
     * @param userId
     * @return
     */
    default UserClockDate getByClasseDateAndUserId(Date classeDate, String userId) {
        assertParamNotNull(classeDate,"classeDate 不能为空");
        assertParamNotEmpty(userId,"userId 不能为空");

        return getOne(Wrappers.<UserClockDate>lambdaQuery().eq(UserClockDate::getUserId, userId).eq(UserClockDate::getClasseDate, DateUtil.formatDate(classeDate)));
    }

    /**
     * 添加用户打卡日期
     * @param classeDate
     * @param userId
     * @return 存在返回已存在的，不存在添加
     */
    default UserClockDate addUserClockDateIfNecessary(Date classeDate,String userId){
        UserClockDate userClockDate = getByClasseDateAndUserId(classeDate, userId);
        if (userClockDate == null) {
            userClockDate = new UserClockDate();
            userClockDate.setUserId(userId);
            userClockDate.setClasseDate(classeDate);
            // 添加时默认没有异常
            userClockDate.setIsHasError(false);
            userClockDate = insert(userClockDate);
        }

        return userClockDate;
    }

    default boolean updateHasErrorField(String userClockDateId, boolean hasError) {
        assertParamNotEmpty(userClockDateId,"userClockDateId不能为空");
        UserClockDate userClockDate = new UserClockDate();
        userClockDate.setId(userClockDateId);

        userClockDate.setIsHasError(hasError);
        return updateById(userClockDate);
    }
}
